package interfaz;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.ServerSocket;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.Border;

import mundo.P2P;

public class DialogoAgregarAplicacion extends JDialog implements ActionListener
{
	private JTextField txtNombre;
	private JComboBox comboClase;
	private JTextField txtPuerto;
	private JButton btnAgregar;
	private JButton btnLimpiar;
	
	private InterfazP2P interfaz;
	
	public DialogoAgregarAplicacion( InterfazP2P i )
	{
		interfaz = i;
		setLocation( (int)Toolkit.getDefaultToolkit().getScreenSize().getWidth()/2, (int)Toolkit.getDefaultToolkit().getScreenSize().getHeight()/2 );
		JPanel panel = new JPanel( new GridBagLayout( ) );
		setSize( new Dimension( 300,200 ));
		setResizable( false );
		setTitle( "Agregar Nueva Aplicacion" );
		panel.setSize( new Dimension( 500,250 ));
		
		
		txtNombre = new JTextField( 30 );
		comboClase = new JComboBox( new String[]{ "EsPrimo", "Substring", "FileTransfer", "Time" } );
		txtPuerto = new JTextField( 10 );
		txtPuerto.setText( P2P.darPuertoLibre() + "" );
		
		btnAgregar = new JButton( "Agregar Aplicacion" );
		btnAgregar.addActionListener( this );
		btnAgregar.setActionCommand( "AGREGAR" );
		
		btnLimpiar = new JButton( "Limpiar Forma" );
		btnLimpiar.addActionListener( this );
		btnLimpiar.setActionCommand( "LIMPIAR" );
		
		JLabel label = null;
		
		GridBagConstraints c = new GridBagConstraints( );
		c.fill = GridBagConstraints.BOTH;
		c.insets = new Insets( 5,5,5,5 );
		
		c.weightx = 0;
		c.ipadx = 5;
		c.ipady = 5;
		label = new JLabel( "Nombre Aplicacion" );
		panel.add( label, c );
		c.gridx = 1;
		c.weightx = 1;
		panel.add( txtNombre, c );
		
		c.weightx = 0;
		c.gridx = 0;
		c.gridy = 1;
		label = new JLabel( "Nombre Clase" );
		panel.add( label, c );
		c.gridx = 1;
		c.weightx = 1;
		panel.add( comboClase, c );
		
		c.weightx = 0;
		c.gridx = 0;
		c.gridy = 2;
		c.gridwidth = 1;
		label = new JLabel( "Puerto" );
		panel.add( label, c );
		c.gridx = 1;
		c.weightx = 1;
		panel.add( txtPuerto, c );
		
		c.gridy = 3;
		c.gridx = 0;
		panel.add( btnAgregar, c );
		c.gridx = 1;
		panel.add( btnLimpiar, c );
		
		add( panel );
		
		
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		String cmd = e.getActionCommand();
		
		if( cmd.equals("AGREGAR") )
			interfaz.agregarAplicacion( this, txtNombre.getText(), (String)comboClase.getSelectedItem(), txtPuerto.getText() );
		else if( cmd.equals("LIMPIAR") )
		{
			txtNombre.setText("");
			txtPuerto.setText("");
		}
	}
	
	


}
